package fr.imag.ufrima.ima.project.ui.tools;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.log4j.Logger;

import fr.imag.ufrima.ima.project.core.Histogram;
import fr.imag.ufrima.ima.project.core.operations.GrayScale;
import fr.imag.ufrima.ima.project.ui.main.ImageProject;

public class HistogramTool extends JPanel implements PropertyChangeListener {
	private Logger logger = Logger.getLogger(getClass().getName());
	private ImageProject imageProject;

	private Histogram histogramRed;
	private JSlider sliderRed;
	private JLabel lblIntensityRed;
	private JButton btnModifyRed;
	private JButton btnRepartHistoRed;
	
	private Histogram histogramGreen;
	private JSlider sliderGreen;
	private JLabel lblIntensityGreen;
	private JButton btnModifyGreen;
	private JButton btnRepartHistoGreen;
	
	private Histogram histogramBlue;
	private JSlider sliderBlue;
	private JLabel lblIntensityBlue;
	private JButton btnModifyBlue;
	private JButton btnRepartHistoBlue;
	
	private JButton btnEgaliserRed;
	private JButton btnEgaliserBleu;
	private JButton btnEgaliserGreen;
		
	private JButton btnEqualizeGS;

	/**
	 * Create the panel.
	 */
	public HistogramTool(ImageProject imageProject) {
		this.imageProject = imageProject;

		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 180 };
		gbl_panel.rowHeights = new int[] { 0, 100, 0, 0, 100, 0, 0, 100, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 1.0 };
		gbl_panel.rowWeights = new double[] { 1.0 };
		panel.setLayout(gbl_panel);

		// Boutton histo red
		JLabel lblTitle = new JLabel("Histograms");
		GridBagConstraints gbc_btnHistoR = new GridBagConstraints();
		gbc_btnHistoR.insets = new Insets(5, 0, 5, 0);
		gbc_btnHistoR.fill = GridBagConstraints.BOTH;
		gbc_btnHistoR.gridx = 0;
		gbc_btnHistoR.gridy = 0;
		panel.add(lblTitle, gbc_btnHistoR);

		histogramRed = new Histogram();
		histogramRed.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_pnlHistogramRed = new GridBagConstraints();
		gbc_pnlHistogramRed.insets = new Insets(5, 5, 5, 5);
		gbc_pnlHistogramRed.fill = GridBagConstraints.BOTH;
		gbc_pnlHistogramRed.gridx = 0;
		gbc_pnlHistogramRed.gridy = 1;
		panel.add(histogramRed, gbc_pnlHistogramRed);

		JPanel panel_11 = new JPanel();
		GridBagLayout gbl_panel_11 = new GridBagLayout();
		gbl_panel_11.columnWidths = new int[] { 120, 20, 60 };
		gbl_panel_11.rowHeights = new int[] { 0 };
		gbl_panel_11.columnWeights = new double[] { 0.5, 0.2, 0.3 };
		gbl_panel_11.rowWeights = new double[] { 1.0 };
		panel_11.setLayout(gbl_panel_11);
		GridBagConstraints gbc_panel_11 = new GridBagConstraints();
		gbc_panel_11.insets = new Insets(5, 5, 5, 5);
		gbc_panel_11.fill = GridBagConstraints.BOTH;
		gbc_panel_11.gridx = 0;
		gbc_panel_11.gridy = 2;
		panel.add(panel_11, gbc_panel_11);
		
		sliderRed = new JSlider(0, 255);
		sliderRed.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				lblIntensityRed.setText(Integer.toString(sliderRed.getValue()));
			}
		});
		GridBagConstraints gbc_sliderRed = new GridBagConstraints();
		gbc_sliderRed.insets = new Insets(5, 0, 5, 0);
		gbc_sliderRed.fill = GridBagConstraints.BOTH;
		gbc_sliderRed.gridx = 0;
		gbc_sliderRed.gridy = 0;
		panel_11.add(sliderRed, gbc_sliderRed);

		lblIntensityRed = new JLabel(Integer.toString(sliderRed.getValue()));
		GridBagConstraints gbc_lblIntensityRed = new GridBagConstraints();
		gbc_lblIntensityRed.anchor = GridBagConstraints.WEST;
		gbc_lblIntensityRed.insets = new Insets(5, 5, 5, 5);
		gbc_lblIntensityRed.gridx = 1;
		gbc_lblIntensityRed.gridy = 0;
		panel_11.add(lblIntensityRed, gbc_lblIntensityRed);

		
		// Boutton appliquer changement red
		btnModifyRed = new JButton("Modify");
		btnModifyRed.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.modifyRed();
			}
		});
		GridBagConstraints gbc_appChang = new GridBagConstraints();
		gbc_appChang.anchor = GridBagConstraints.EAST;
		gbc_appChang.insets = new Insets(5, 0, 5, 0);
		gbc_appChang.fill = GridBagConstraints.NONE;
		gbc_appChang.gridx = 2;
		gbc_appChang.gridy = 0;
		panel_11.add(btnModifyRed, gbc_appChang);
		
		JPanel panel_1 = new JPanel();
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[] { 60, 60, 60 };
		gbl_panel_1.rowHeights = new int[] { 0 };
		gbl_panel_1.columnWeights = new double[] { 0.5, 0.25, 0.25 };
		gbl_panel_1.rowWeights = new double[] { 1.0 };
		panel_1.setLayout(gbl_panel_1);
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(5, 5, 5, 5);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 3;
		panel.add(panel_1, gbc_panel_1);
		
		//repartion histo bouton red
		btnRepartHistoRed = new JButton("Repartition");
		btnRepartHistoRed.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HistogramTool.this.reparthistRed();
			}
		});
		GridBagConstraints gbc_appEqualizeRed = new GridBagConstraints();
		gbc_appEqualizeRed.anchor = GridBagConstraints.EAST;
		gbc_appEqualizeRed.insets = new Insets(0, 0, 5, 0);		
		gbc_appEqualizeRed.gridx = 1;
		gbc_appEqualizeRed.gridy = 0;
		panel_1.add(btnRepartHistoRed, gbc_appEqualizeRed);
										
		// Boutton egaliser red
		btnEgaliserRed = new JButton("Égaliser");
		btnEgaliserRed.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.egaliserRed();
			}
		});
		GridBagConstraints gbc_EgaliserRed = new GridBagConstraints();
		gbc_EgaliserRed.anchor = GridBagConstraints.EAST;
		gbc_EgaliserRed.insets = new Insets(0, 0, 5, 0);
		gbc_EgaliserRed.gridx = 2;
		gbc_EgaliserRed.gridy = 0;
		panel_1.add(btnEgaliserRed, gbc_EgaliserRed);
		
		// Boutton histo green
		histogramGreen = new Histogram();
		histogramGreen.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_pnlHistogramGreen = new GridBagConstraints();
		gbc_pnlHistogramGreen.insets = new Insets(5, 5, 5, 5);
		gbc_pnlHistogramGreen.fill = GridBagConstraints.BOTH;
		gbc_pnlHistogramGreen.gridx = 0;
		gbc_pnlHistogramGreen.gridy = 4;
		panel.add(histogramGreen, gbc_pnlHistogramGreen);

		JPanel panel_12 = new JPanel();
		GridBagLayout gbl_panel_12 = new GridBagLayout();
		gbl_panel_12.columnWidths = new int[] { 120, 20, 60 };
		gbl_panel_12.rowHeights = new int[] { 0 };
		gbl_panel_12.columnWeights = new double[] { 0.5, 0.2, 0.3 };
		gbl_panel_12.rowWeights = new double[] { 1.0 };
		panel_12.setLayout(gbl_panel_12);
		GridBagConstraints gbc_panel_12= new GridBagConstraints();
		gbc_panel_12.insets = new Insets(5, 5, 5, 5);
		gbc_panel_12.fill = GridBagConstraints.BOTH;
		gbc_panel_12.gridx = 0;
		gbc_panel_12.gridy = 5;
		panel.add(panel_12, gbc_panel_12);
		
		sliderGreen = new JSlider(0, 255);
		sliderGreen.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				lblIntensityGreen.setText(Integer.toString(sliderGreen.getValue()));
			}
		});
		GridBagConstraints gbc_sliderGreen = new GridBagConstraints();
		gbc_sliderGreen.insets = new Insets(5, 0, 5, 0);
		gbc_sliderGreen.fill = GridBagConstraints.BOTH;
		gbc_sliderGreen.gridx = 0;
		gbc_sliderGreen.gridy = 0;
		panel_12.add(sliderGreen, gbc_sliderGreen);

		lblIntensityGreen = new JLabel(Integer.toString(sliderRed.getValue()));
		GridBagConstraints gbc_lblIntensityGreen = new GridBagConstraints();
		gbc_lblIntensityGreen.anchor = GridBagConstraints.WEST;
		gbc_lblIntensityGreen.fill = GridBagConstraints.NONE;
		gbc_lblIntensityGreen.insets = new Insets(5, 5, 5, 5);
		gbc_lblIntensityGreen.gridx = 1;
		gbc_lblIntensityGreen.gridy = 0;
		panel_12.add(lblIntensityGreen, gbc_lblIntensityGreen);
						
		// Boutton appliquer changement green
		btnModifyGreen = new JButton("Modify");
		btnModifyGreen.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.modifyGreen();
			}
		});
		GridBagConstraints gbc_appChangGreen = new GridBagConstraints();
		gbc_appChangGreen.anchor = GridBagConstraints.EAST;
		gbc_appChangGreen.insets = new Insets(0, 0, 5, 0);
		gbc_appChangGreen.fill = GridBagConstraints.NONE;
		gbc_appChangGreen.gridx = 2;
		gbc_appChangGreen.gridy = 0;
		panel_12.add(btnModifyGreen, gbc_appChangGreen);
		
		JPanel panel_2 = new JPanel();
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[] { 60, 60, 60 };
		gbl_panel_2.rowHeights = new int[] { 0 };
		gbl_panel_2.columnWeights = new double[] { 0.5, 0.25, 0.25 };
		gbl_panel_2.rowWeights = new double[] { 1.0 };
		panel_2.setLayout(gbl_panel_2);
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.insets = new Insets(5, 5, 5, 5);
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 6;
		panel.add(panel_2, gbc_panel_2);
				
		//repartion histo bouton Green
		btnRepartHistoGreen = new JButton("Repartition");
		btnRepartHistoGreen.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HistogramTool.this.reparthistGreen();
			}
		});
		GridBagConstraints gbc_appEqualizeGreen = new GridBagConstraints();
		gbc_appEqualizeGreen.anchor = GridBagConstraints.EAST;
		gbc_appEqualizeGreen.fill = GridBagConstraints.NONE;
		gbc_appEqualizeGreen.insets = new Insets(0, 0, 5, 0);		
		gbc_appEqualizeGreen.gridx = 1;
		gbc_appEqualizeGreen.gridy = 0;
		panel_2.add(btnRepartHistoGreen, gbc_appEqualizeGreen);
		
		// Boutton egaliser Green
		btnEgaliserGreen = new JButton("Égaliser");
		btnEgaliserGreen.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.egaliserGreen();
			}
		});
		GridBagConstraints gbc_EgaliserGreen = new GridBagConstraints();
		gbc_EgaliserGreen.anchor = GridBagConstraints.EAST;
		gbc_EgaliserGreen.insets = new Insets(0, 0, 5, 0);
		gbc_EgaliserGreen.fill = GridBagConstraints.NONE;
		gbc_EgaliserGreen.gridx = 2;
		gbc_EgaliserGreen.gridy = 0;
		panel_2.add(btnEgaliserGreen, gbc_EgaliserGreen);
				
		// Boutton histo blue
		histogramBlue = new Histogram();
		histogramBlue.setBorder(new LineBorder(new Color(0, 0, 0)));
		GridBagConstraints gbc_pnlHistogramBlue = new GridBagConstraints();
		gbc_pnlHistogramBlue.insets = new Insets(5, 5, 5, 5);
		gbc_pnlHistogramBlue.fill = GridBagConstraints.BOTH;
		gbc_pnlHistogramBlue.gridx = 0;
		gbc_pnlHistogramBlue.gridy = 7;
		panel.add(histogramBlue, gbc_pnlHistogramBlue);

		JPanel panel_13 = new JPanel();
		GridBagLayout gbl_panel_13 = new GridBagLayout();
		gbl_panel_13.columnWidths = new int[] { 120, 20, 60 };
		gbl_panel_13.rowHeights = new int[] { 0 };
		gbl_panel_13.columnWeights = new double[] { 0.5, 0.2, 0.3 };
		gbl_panel_13.rowWeights = new double[] { 1.0 };
		panel_13.setLayout(gbl_panel_13);
		GridBagConstraints gbc_panel_13= new GridBagConstraints();
		gbc_panel_13.insets = new Insets(5, 5, 5, 5);
		gbc_panel_13.fill = GridBagConstraints.BOTH;
		gbc_panel_13.gridx = 0;
		gbc_panel_13.gridy = 8;
		panel.add(panel_13, gbc_panel_13);
		
		sliderBlue = new JSlider(0, 255);
		sliderBlue.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				lblIntensityBlue.setText(Integer.toString(sliderBlue.getValue()));
			}
		});
		GridBagConstraints gbc_sliderBlue = new GridBagConstraints();
		gbc_sliderBlue.insets = new Insets(5, 0, 5, 0);
		gbc_sliderBlue.fill = GridBagConstraints.BOTH;
		gbc_sliderBlue.gridx = 0;
		gbc_sliderBlue.gridy = 0;
		panel_13.add(sliderBlue, gbc_sliderBlue);

		lblIntensityBlue = new JLabel(Integer.toString(sliderRed.getValue()));
		GridBagConstraints gbc_lblIntensityBlue = new GridBagConstraints();
		gbc_lblIntensityBlue.anchor = GridBagConstraints.WEST;
		gbc_lblIntensityBlue.fill = GridBagConstraints.NONE;
		gbc_lblIntensityBlue.insets = new Insets(5, 5, 5, 5);
		gbc_lblIntensityBlue.gridx = 1;
		gbc_lblIntensityBlue.gridy = 0;
		panel_13.add(lblIntensityBlue, gbc_lblIntensityBlue);
		
		// Boutton appliquer changement bleu
		btnModifyBlue = new JButton("Modify");
		btnModifyBlue.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.modifyBlue();
			}
		});
		GridBagConstraints gbc_appChangBlue = new GridBagConstraints();
		gbc_appChangBlue.anchor = GridBagConstraints.EAST;
		gbc_appChangBlue.fill = GridBagConstraints.NONE;
		gbc_appChangBlue.insets = new Insets(0, 0, 5, 0);		
		gbc_appChangBlue.gridx = 2;
		gbc_appChangBlue.gridy = 0;
		panel_13.add(btnModifyBlue, gbc_appChangBlue);
		
		JPanel panel_3 = new JPanel();
		GridBagLayout gbl_panel_3 = new GridBagLayout();
		gbl_panel_3.columnWidths = new int[] { 60, 60, 60 };
		gbl_panel_3.rowHeights = new int[] { 0 };
		gbl_panel_3.columnWeights = new double[] { 0.5, 0.25, 0.25 };
		gbl_panel_3.rowWeights = new double[] { 1.0 };
		panel_3.setLayout(gbl_panel_3);
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(5, 5, 5, 5);
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 0;
		gbc_panel_3.gridy = 9;
		panel.add(panel_3, gbc_panel_3);	
		
		//ega=aliser bouton bleu
		btnRepartHistoBlue = new JButton("Repartition");
		btnRepartHistoBlue.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				HistogramTool.this.reparthistBlue();
			}
		});
		GridBagConstraints gbc_appEqualizeBlue = new GridBagConstraints();
		gbc_appEqualizeBlue.anchor = GridBagConstraints.EAST;
		gbc_appEqualizeBlue.fill = GridBagConstraints.NONE;
		gbc_appEqualizeBlue.insets = new Insets(0, 0, 5, 0);		
		gbc_appEqualizeBlue.gridx = 1;
		gbc_appEqualizeBlue.gridy = 0;
		panel_3.add(btnRepartHistoBlue, gbc_appEqualizeBlue);
						
		// Boutton egaliser Green
		btnEgaliserBleu = new JButton("Égaliser");
		btnEgaliserBleu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.egaliserBleu();
			}
		});
		GridBagConstraints gbc_EgaliserBleu = new GridBagConstraints();
		gbc_EgaliserBleu.anchor = GridBagConstraints.EAST;
		gbc_EgaliserBleu.insets = new Insets(0, 0, 5, 0);
		gbc_EgaliserBleu.fill = GridBagConstraints.NONE;
		gbc_EgaliserBleu.gridx = 2;
		gbc_EgaliserBleu.gridy = 0;
		panel_3.add(btnEgaliserBleu, gbc_EgaliserBleu);
				
		// Boutton egaliser niv gris
		btnEqualizeGS = new JButton("Égaliser Gris");
		btnEqualizeGS.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				HistogramTool.this.egaliserGS();
			}
		});
		GridBagConstraints gbc_appChangGs = new GridBagConstraints();
		gbc_appChangGs.anchor = GridBagConstraints.EAST;
		gbc_appChangGs.fill = GridBagConstraints.NONE;
		gbc_appChangGs.insets = new Insets(0, 0, 5, 0);		
		gbc_appChangGs.gridx = 0;
		gbc_appChangGs.gridy = 10;
		panel.add(btnEqualizeGS, gbc_appChangGs);
	}

	
	
	@Override
	public void paint(Graphics arg0) {
		// TODO Auto-generated method stub
		super.paint(arg0);

		histogramRed.repaint();
	}

	private void initTool() {
		histogramRed.createFromBufferedImage(HistogramTool.this.imageProject.getCurrentImageDocument()
				.getBufferedImage(), Color.RED);
		//sliderRed.setValue(histogramRed.getHistogramMean());
		sliderRed.setValue(128);

		histogramGreen.createFromBufferedImage(HistogramTool.this.imageProject.getCurrentImageDocument()
				.getBufferedImage(), Color.GREEN);
		sliderGreen.setValue(128);
		
		histogramBlue.createFromBufferedImage(HistogramTool.this.imageProject.getCurrentImageDocument()
				.getBufferedImage(), Color.BLUE);
		sliderBlue.setValue(128);
	}

	private void modifyRed() {
		int[] recalculatedHistogram = histogramRed.changeHistogramMean(sliderRed.getValue());
		histogramRed.createFromArray(recalculatedHistogram, Color.RED);
		histogramRed.repaint();

		BufferedImage resultImage = histogramRed.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}

	private void modifyGreen() {
		int[] recalculatedHistogram = histogramGreen.changeHistogramMean(sliderGreen.getValue());
		histogramGreen.createFromArray(recalculatedHistogram, Color.GREEN);
		histogramGreen.repaint();

		BufferedImage resultImage = histogramGreen.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}

	private void modifyBlue() {
		int[] recalculatedHistogram = histogramBlue.changeHistogramMean(sliderBlue.getValue());
		histogramBlue.createFromArray(recalculatedHistogram, Color.BLUE);
		histogramBlue.repaint();

		BufferedImage resultImage = histogramBlue.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}
	
	
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
	
	private void reparthistBlue() {
		int[] recalculatedHistogram = histogramBlue.regalize();//recuperer le nouveau tab
		
		histogramBlue.createFromArray(recalculatedHistogram, Color.BLUE);
		histogramBlue.repaint();
		
		BufferedImage resultImage = histogramBlue.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}
	
	private void reparthistGreen() {
		int[] recalculatedHistogram = histogramGreen.regalize();//recuperer le nouveau tab
		
		histogramGreen.createFromArray(recalculatedHistogram, Color.GREEN);
		histogramGreen.repaint();
		
		BufferedImage resultImage = histogramGreen.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}

	private void reparthistRed() {
		int[] recalculatedHistogram = histogramRed.regalize();//recuperer le nouveau tab
		
		histogramRed.createFromArray(recalculatedHistogram, Color.RED);
		histogramRed.repaint();
		
		BufferedImage resultImage = histogramRed.modifImagByHisto(imageProject.getCurrentImageDocument().getImage());
		imageProject.getCurrentImageDocument().getImage().modifyWithImage(resultImage);

		this.imageProject.getCurrentImageDocument().repaint();
	}

	private void egaliserGS() {
		
		GrayScale grayScale = new GrayScale(imageProject.getCurrentImageDocument().getImage());
		if(grayScale.isInGray()){
			System.out.println("what");
			int[] hisng=new int[256];
			int[] hisng2=new int[256];
			int[] cumul=new int[256];
			int[] cumul2=new int[256];
			int h=imageProject.getCurrentImageDocument().getImage().getHeight();
			int w=imageProject.getCurrentImageDocument().getImage().getWidth();
			
				
			int[][][] tab=imageProject.getCurrentImageDocument().getImage().getTab();
			int[][][] tabres=imageProject.getCurrentImageDocument().getImage().getTab();
			BufferedImage biG = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
			
	
			//calcul histo norm
			for (int j = 0; j < h; j++) {
				for (int i = 0; i < w; i++) {			
					hisng[(tab[i][j][2])]++;
				}
			}
			
			//histo cumul
			for (int j = 0; j < h; j++) {
				for (int i = 0; i < w; i++) {			
					for(int k=255;k>tab[i][j][2];k--)
						cumul[k]++;
				}
			}
			
			//legal de limage
			for (int j = 0; j < h; j++) {
				for (int i = 0; i < w; i++) {			
					tabres[i][j][2]=(255*cumul[tab[i][j][2]]);
					tabres[i][j][2]/=(h*w);
				}
			}
			
		
			
			//calcul histo vrai apres changement
			for (int j = 0; j < h; j++) {
				for (int i = 0; i < w; i++) {			
					hisng2[(tab[i][j][2])]++;
				}
			}

			for (int j = 0; j < h; j++) {
				for (int i = 0; i < w; i++) {
					biG.getRaster().getDataBuffer().setElem(j * w + i, tabres[i][j][2]);
				}
			}
			
			histogramBlue.createFromArray(hisng2, Color.BLACK);
			histogramBlue.repaint();
			imageProject.getCurrentImageDocument().getImage().setBufferedImage(biG);
			this.imageProject.getCurrentImageDocument().repaint();
		}
	}
		
	private void egaliserRed() {
			
		GrayScale grayScale = new GrayScale(imageProject.getCurrentImageDocument().getImage());
			if(!grayScale.isInGray()){
				System.out.println("egalisation de l'image..");
				
				int[] hisng=new int[256];
				int[] hisng2=new int[256];
				int[] cumul=new int[256];
				int[] cumul2=new int[256];
				
				int h=imageProject.getCurrentImageDocument().getImage().getHeight();
				int w=imageProject.getCurrentImageDocument().getImage().getWidth();
				
				
				int[][][] tab=imageProject.getCurrentImageDocument().getImage().getTab();
				int[][][] tabres=imageProject.getCurrentImageDocument().getImage().getTab();
				
				BufferedImage biN = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
				//calcul histo norm
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng[(tab[i][j][1])]++;
					}
				}
				
				//histo cumul
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=255;k>tab[i][j][1];k--)
							cumul[k]++;
					}
				}
				
				//legal de limage
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						tabres[i][j][1]=(255*cumul[tab[i][j][1]]);
						tabres[i][j][1]/=(h*w);
					}
				}
				
				//histo cumul 2
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=0;k<tabres[i][j][1];k++)
							cumul2[k]++;
					}
				}
				
				//calcul histo vrai apres changement
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng2[(tab[i][j][1])]++;
					}
				}

				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {
						int pval1, pval2, pval3, pval0; // operation intiale
						pval0 = (tabres[i][j][0]);
						pval1 = (tabres[i][j][1]);
						pval2 = (tabres[i][j][2]);
						pval3 = (tabres[i][j][3]);
						int val = (pval0 << 24) + (pval1 << 16) + (pval2 << 8) + (pval3);
						
						biN.setRGB(i, j, val);
					}
				}		
				histogramRed.createFromArray(hisng2, Color.RED);
				histogramRed.repaint();				
				imageProject.getCurrentImageDocument().getImage().setBufferedImage(biN);
				this.imageProject.getCurrentImageDocument().repaint();
			}
		
		
		else
			System.out.println("image au gray niveau ! impo");
	}
	
	private void egaliserGreen() {		
		GrayScale grayScale = new GrayScale(imageProject.getCurrentImageDocument().getImage());
		if(!grayScale.isInGray()){
				System.out.println("egalisation de l'image..");
				
				int[] hisng=new int[256];
				int[] hisng2=new int[256];
				int[] cumul=new int[256];
				int[] cumul2=new int[256];
				
				int h=imageProject.getCurrentImageDocument().getImage().getHeight();
				int w=imageProject.getCurrentImageDocument().getImage().getWidth();
				
				
				int[][][] tab=imageProject.getCurrentImageDocument().getImage().getTab();
				int[][][] tabres=imageProject.getCurrentImageDocument().getImage().getTab();
				
				BufferedImage biN = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
				//calcul histo norm
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng[(tab[i][j][2])]++;
					}
				}
				
				//histo cumul
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=255;k>tab[i][j][2];k--)
							cumul[k]++;
					}
				}
				
				//legal de limage
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						tabres[i][j][1]=(255*cumul[tab[i][j][2]]);
						tabres[i][j][1]/=(h*w);
					}
				}
				
				//histo cumul 2
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=0;k<tabres[i][j][2];k++)
							cumul2[k]++;
					}
				}
				
				//calcul histo vrai apres changement
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng2[(tab[i][j][2])]++;
					}
				}

				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {
						int pval1, pval2, pval3, pval0; // operation intiale
						pval0 = (tabres[i][j][0]);
						pval1 = (tabres[i][j][1]);
						pval2 = (tabres[i][j][2]);
						pval3 = (tabres[i][j][3]);
						int val = (pval0 << 24) + (pval1 << 16) + (pval2 << 8) + (pval3);
						
						biN.setRGB(i, j, val);
					}
				}		
				histogramGreen.createFromArray(hisng2, Color.GREEN);
				histogramGreen.repaint();				
				imageProject.getCurrentImageDocument().getImage().setBufferedImage(biN);
				this.imageProject.getCurrentImageDocument().repaint();
			}
	
		else
			System.out.println("image au gray niveau ! impo");
	}
		
	private void egaliserBleu(){
			
		GrayScale grayScale = new GrayScale(imageProject.getCurrentImageDocument().getImage());
		if(!grayScale.isInGray()){
				System.out.println("egalisation de l'image..");
				
				int[] hisng=new int[256];
				int[] hisng2=new int[256];
				int[] cumul=new int[256];
				int[] cumul2=new int[256];
				
				int h=imageProject.getCurrentImageDocument().getImage().getHeight();
				int w=imageProject.getCurrentImageDocument().getImage().getWidth();
				
				
				int[][][] tab=imageProject.getCurrentImageDocument().getImage().getTab();
				int[][][] tabres=imageProject.getCurrentImageDocument().getImage().getTab();
				
				BufferedImage biN = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
				//calcul histo norm
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng[(tab[i][j][3])]++;
					}
				}
				
				//histo cumul
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=255;k>tab[i][j][3];k--)
							cumul[k]++;
					}
				}
				
				//legal de limage
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						tabres[i][j][1]=(255*cumul[tab[i][j][3]]);
						tabres[i][j][1]/=(h*w);
					}
				}
				
				//histo cumul 2
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						for(int k=0;k<tabres[i][j][3];k++)
							cumul2[k]++;
					}
				}
				
//calcul histo vrai apres changement
				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {			
						hisng2[(tab[i][j][3])]++;
					}
				}

				for (int j = 0; j < h; j++) {
					for (int i = 0; i < w; i++) {
						int pval1, pval2, pval3, pval0; // operation intiale
						pval0 = (tabres[i][j][0]);
						pval1 = (tabres[i][j][1]);
						pval2 = (tabres[i][j][2]);
						pval3 = (tabres[i][j][3]);
						int val = (pval0 << 24) + (pval1 << 16) + (pval2 << 8) + (pval3);
						
						biN.setRGB(i, j, val);
					}
				}		
				histogramBlue.createFromArray(hisng2, Color.BLUE);
				histogramBlue.repaint();				
				imageProject.getCurrentImageDocument().getImage().setBufferedImage(biN);
				this.imageProject.getCurrentImageDocument().repaint();
			}
		else
			System.out.println("image au gray niveau ! impo");
	}
	
	public void propertyChange(PropertyChangeEvent e) {
		// TODO Auto-generated method stub
		String propertyName = e.getPropertyName();
		if ("visible".equals(propertyName)) {
			if ("true".equals(e.getNewValue().toString())) {
				logger.debug(String.format("HistogramTool has become visible.", propertyName));
				initTool();
			}
		}
	}

	public PropertyChangeListener getPropertyChangeListener() {
		return this;
	}
}
